package com.zsr.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsr.example.entity.EduCourse;
import com.zsr.example.entity.EduSection;
import com.zsr.example.mapper.EduVideoMapper;
import com.zsr.example.service.EduCourseService;
import com.zsr.example.service.EduSectionService;
import com.zsr.example.service.TOrderService;
import com.zsr.example.service.VodService;
import com.zsr.example.util.JwtUtil;
import com.zsr.example.util.constants.BusinessEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author zsr
 * @since 2021-11-15
 */
@Service
public class EduSectionServiceImpl extends ServiceImpl<EduVideoMapper, EduSection> implements EduSectionService {

    @Autowired
    private VodService vodService;
    @Autowired
    private TOrderService orderService;
    @Autowired
    private EduCourseService courseService;

    /**
     * 根据章节id查询小节是否有数据
     *
     * @param id 章节id
     * @return
     */
    @Override
    public boolean getCountByChapterId(String id) {
        QueryWrapper<EduSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id", id);
        Integer count = baseMapper.selectCount(queryWrapper);
        return null != count && count > 0;
    }

    //根据课程id删除小节
    @Override
    public void removeVideoByCourseId(String courseId) {
        //TODO  批量删除视频[已完成]
        //根据课程id查询所有视频列表
        QueryWrapper<EduSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        queryWrapper.select("video_source_id");
        List<EduSection> videoList = this.list(queryWrapper);

        List<String> videoIds = videoList.stream().map(EduSection::getVideoSourceId).collect(Collectors.toList());

        if (videoIds.size() > 0) {
            vodService.removeVideoList(videoIds);
        }


        QueryWrapper<EduSection> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id", courseId);
        this.remove(videoWrapper);

    }

    //TODO 删除小节下的视频
    //根据小节id删除小节和视频
    @Override
    public boolean removeVideoById(String id) {
        // 根据小节id获取视频id，调用方法实现视频删除
        EduSection eduVideo = this.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();

        // 判断小节里面是否有视频id
        if (!StringUtils.isEmpty(videoSourceId)) {
            // 根据视频id,远程调用实现视频删除
            vodService.removeVideo(videoSourceId);
        }
        return this.removeById(id);
    }

    @Override
    public boolean isHavePermissions(String videoSourceId, String memberId) {
        boolean isHavePermissions = false;
        EduSection section = baseMapper.selectOne(new LambdaQueryWrapper<EduSection>().eq(EduSection::getVideoSourceId, videoSourceId));
        if (section != null) {
            if (section.getIsFree()) {
                isHavePermissions = true;
            } else {
                String courseId = section.getCourseId();
                if (!StringUtils.isEmpty(memberId)) {
                    //是否购买
                    isHavePermissions = orderService.isBuyCourse(memberId, BusinessEnum.COURSE.getType(), courseId);
                } else {
                    EduCourse course = courseService.getById(section.getCourseId());
                    //是否免费
                    isHavePermissions = course.getPrice().compareTo(BigDecimal.ZERO) == 0;
                }
            }
        }
        return isHavePermissions;
    }
}
